﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GameControlTest
{
    public partial class GameControlCompositionTargetRendering : UserControl
    {
        private List<GameObject> _gameObjects;
        private GameObject _player;
        private DateTime _lastTime;
        private int _xDir = 1;
        private int _yDir = 1;

        private static Random s_random = new Random();

        public GameControlCompositionTargetRendering()
        {
            InitializeComponent();
            _gameObjects = new List<GameObject>();

            for (int i = 0; i < App.ObjectCount; i++)
            {
                GameObject go = new GameObject(new Rect(s_random.Next(800) - 400,
                    s_random.Next(600) - 300, App.RectSize, App.RectSize), Colors.Black);
                _gameObjects.Add(go);
                this.LayoutRoot.Children.Add(go.DrawRectangle);
            }
            _player = _gameObjects[0];

            _lastTime = DateTime.Now;

            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);

            this.KeyDown += new KeyEventHandler(GameControlCompositionTargetRendering_KeyDown);
        }

        private void GameControlCompositionTargetRendering_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Up)
            {
                _xDir *= -1;
                _player.Translate(0, -5);
            }
            if (e.Key == Key.Down)
            {
                _yDir *= -1;
                _player.Translate(0, 5);
            }
            if (e.Key == Key.Left)
            {
                _yDir *= -1;
                _player.Translate(-5, 0);
            }
            if (e.Key == Key.Right)
            {
                _yDir *= -1;
                _player.Translate(5, 0);
            }
        }

        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            TimeSpan elapsedTime = now - _lastTime;
            _lastTime = now;
            float elapsedTimeRatio = (float)elapsedTime.TotalSeconds / ((float)60 / 1000);

            Update(elapsedTimeRatio);
            CheckForCollisions();
        }

        private void Update(float elapsedTimeRatio)
        {
            for(int i = 0; i < _gameObjects.Count; i++)
            {
                _gameObjects[i].Update(_xDir, _yDir, elapsedTimeRatio);
            }
        }

        private void CheckForCollisions()
        {
            _gameObjects = _gameObjects.OrderBy(x => x.CollisionRect.X).ToList();
            for (int i = 0; i < _gameObjects.Count; i++)
            {
                GameObject a = _gameObjects[i];
                for (int j = i + 1; j < _gameObjects.Count && _gameObjects[j].CollisionRect.Left < a.CollisionRect.Right; j++)
                {
                    GameObject b = _gameObjects[j];
                    Rect tempRect = a.CollisionRect;
                    tempRect.Intersect(b.CollisionRect);
                    if (!tempRect.IsEmpty)
                    {
                        a.Collided(b);
                        b.Collided(a);
                    }
                }
            }
        }
    }
}
